<?php
namespace App\Http\Controllers\Front;

use App\Service\Front\DesignCaseService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class DesignCaseController extends BaseController
{
    //业务

    public function __construct(DesignCaseService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //分类子集合
    public function cateByPid(Request $request)
    {
        $pid = $request->post('pid', 0);
        $res = $this->db->cateOption(3, $pid);
        return $this->toJson(0, '', $res);
    }

    //地区子集合
    public function areaByPid(Request $request)
    {
        $pid = $request->post('pid', 0);
        $res = $this->db->areaOption($pid);
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $cateid = $request->get('cate_id', 0);
        $pages['start'] = $request->get('page', 1);
        $pages['limit'] = $request->get('limit', 10);
        $res = $this->db->getIndex($cateid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加
    public function add(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $token = $request->post('token', '');
        $pas['cid'] = $this->db->cidByToken($token);
        $pas['title'] = $request->post('title', '');
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['price'] = $request->post('price', 0);
        $pas['area_id'] = $request->post('area_id', 0);
        if (!$pas['cid']) {
            return $this->toJson(-2, '你的企业身份无效');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '项目名称必须的');
        }
        if (!$pas['price']) {
            return $this->toJson(-2, '项目价格必须的');
        }
        $pas['thumb'] = Upload::uploadImg($request, 'thumb');
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}